package cn.com.gridinfo.sooc.core.expand.callname.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * 点名的dao
 *
 * @author yingchunli
 */
public class CallNameDao extends SoocIDao {

    public String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    public String statusCode;//提示信息
    public static ClassInfo classInfo = new ClassInfo();//存储获取的课堂信息
    public List<ClassInfo> classInfoList;//课程信息列表
    public List<Student> studentList;//学生信息列表
    public int pageNo;//页码
    public int pageSize;//每页显示的条数
    public int mdrs;//学生名单总人数
    public int count;//签到列表数目
    public int countCallname;//历史点名总次数
    String studentCount;//缺席统计的总人数
    private List<ClassInfo> myclasses;//该教师创建过的课程
    private String skxq;//创建课程时需要的上课学期
    public Student student = new Student();//
    private String signTime;//补签成功的时间
    private String qdfs;//签到方式

    public CallNameDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if (result == null) return;
        status = result.get("status").asText();
        statusCode = result.get("statusCode").asText();
        if (result.has("pageNo")) pageNo = result.get("pageNo").asInt();
        if (result.has("pageSize")) pageSize = result.get("pageSize").asInt();
        Log.d("result->" + result.toString());

        switch (requestCode) {
            case CodeConst.EXPAND_CREATE_CLASS:
                //创建课堂
                break;
            case CodeConst.EXPAND_GET_CLASS:
                //创建课堂时获取课堂信息
                classInfo.setSckcm(result.get("data").get("sckcm").asText());//上次课程名称
                classInfo.setScktzt(result.get("data").get("scktzt").asText());//上次课堂主题
                classInfo.setScskdd(result.get("data").get("scskdd").asText());//上次上课地点
                classInfo.setSkrq(result.get("data").get("skrq").asText());//上课日期
                classInfo.setSkxq(result.get("data").get("skxq").asText());//上课学期
                myclasses = JsonUtil.node2pojoList(result.get("data").get("myclasses"), ClassInfo.class);//该教师创建过的课程
                break;

            //获取今日点名的列表  首次请求  上拉请求  下拉加载
            case CodeConst.EXPAND_GET_TAODAYCALSS_FRIST:
            case CodeConst.EXPAND_GET_TAODAYCALSS_PULLUP:
            case CodeConst.EXPAND_GET_TAODAYCALSS_PUSHDOWN:
                classInfoList = JsonUtil.node2pojoList(result.get("data"), ClassInfo.class);
                break;

            //获取历史点名列表  首次请求  上拉请求   下拉请求
            case CodeConst.EXPAND_GET_HISTORY_CALLNEM_FRIST:
            case CodeConst.EXPAND_GET_HISTORY_CALLNEM_PULLUP:
            case CodeConst.EXPAND_GET_HISTORY_CALLNEM_PUSHDOWN:
                classInfoList = JsonUtil.node2pojoList(result.get("data"), ClassInfo.class);
                countCallname = result.get("count").asInt();
                break;

            //获取统计列表
            case CodeConst.EXPAND_GET_TONGJI_FRIST:
            case CodeConst.EXPAND_GET_TONGJI_PULLUP:
            case CodeConst.EXPAND_GET_TONGJI_PUSHDOWN:
                classInfoList = JsonUtil.node2pojoList(result.get("data"), ClassInfo.class);
                break;

            //获取学生名单列表  首次请求  上拉请求   下拉请求
            case CodeConst.EXPAND_GET_STUDENTMENU_FRIST:
            case CodeConst.EXPAND_GET_STUDENTMENU_PULLUP:
            case CodeConst.EXPAND_GET_STUDENTMENU_PUSHDOWN:
                studentList = JsonUtil.node2pojoList(result.get("data").get("student"), Student.class);
                mdrs = result.get("data").get("mdrs").asInt();
                Log.d("studentlist->" + studentList.size());
                break;

            //获取统计详情列表   首次请求  上拉请求   下拉请求
            case CodeConst.EXPAND_GET_TONGJIINFO_FRIST:
            case CodeConst.EXPAND_GET_TONGJIINFO_PULLUP:
            case CodeConst.EXPAND_GET_TONGJIINFO_PUSHDOWN:
                classInfoList = JsonUtil.node2pojoList(result.get("data"), ClassInfo.class);
                break;

            //获取缺席统计的列表  首次请求  上拉请求   下拉请求
            case CodeConst.EXPAND_QUQXI_GET_STUDENT_FRIST:
            case CodeConst.EXPAND_QUQXI_GET_STUDENT_PULLUP:
            case CodeConst.EXPAND_QUQXI_GET_STUDENT_PUSHDOWN:
                studentList = JsonUtil.node2pojoList(result.get("data"), Student.class);
                studentCount = result.get("count").asText();
                break;

            //获取缺席课堂的列表  首次请求
            case CodeConst.EXPAND_QUEXI_GET_CLASS_FRIST:
            case CodeConst.EXPAND_QUEXI_GET_CLASS_PULLUP:
            case CodeConst.EXPAND_QUEXI_GET_CLASS_PUSHDOWN:
                classInfoList = JsonUtil.node2pojoList(result.get("data"), ClassInfo.class);
                Log.d("list.size->" + classInfoList.size());
                break;

            //签到详情 实到 首次请求
            case CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST:
            case CodeConst.EXPAND_SIGNINFO_SHIDAO_PULLUP:
            case CodeConst.EXPAND_SIGNINFO_SHIDAO_PUSHDOWN:
                classInfo.setQdrs(result.get("data").get("qdrs").asText());
                studentList = JsonUtil.node2pojoList(result.get("data").get("xssj"), Student.class);
                break;

            //签到详情 应到 首次请求
            case CodeConst.EXPAND_SIGNINFO_YINGDAO_FRIST:
            case CodeConst.EXPAND_SIGNINFO_YINGDAO_PULLUP:
            case CodeConst.EXPAND_SIGNINFO_YINGDAO_PUSHDOWN:
                classInfo.setYdrs(result.get("data").get("ydrs").asText());
                break;

            //获取签到详情   应到 学生列表
            case CodeConst.EXPAND_GET_STUDENT_LIST_FIRST:
            case CodeConst.EXPAND_GET_STUDENT_LIST_PULLUP:
            case CodeConst.EXPAND_GET_STUDENT_LIST_PUSHDOWN:
                mdrs = result.get("data").get("mdrs").asInt();
                studentList = JsonUtil.node2pojoList(result.get("data").get("student"), Student.class);
                break;

            //签到详情 缺席 首次请求
            case CodeConst.EXPAND_SIGNINFO_QUEXI_FRIST:
            case CodeConst.EXPAND_SIGNINFO_QUEXI_PULLUP:
            case CodeConst.EXPAND_SIGNINFO_QUEXI_PUSHDOWN:
                classInfo.setQsrs(result.get("data").get("qsrs").asText());
                classInfo.setYdrs(result.get("data").get("ydrs").asText());
                studentList = JsonUtil.node2pojoList(result.get("data").get("qxsj"), Student.class);
                break;

            case CodeConst.EXPAND_LOOK_CLASSINFO_FRIST:
                //获取课堂详情  签到详情界面   没用
                classInfo = JsonUtil.node2pojo(result.get("data"), ClassInfo.class);
                break;

            //获取签到列表  首次请求   下拉加载  上拉刷新
            case CodeConst.EXPAND_GET_SIGNLIST_FIRST:
            case CodeConst.EXPAND_GET_SIGNLIST_PUSHDOWN:
            case CodeConst.EXPAND_GET_SIGNLIST_PULLUP:
                count = result.get("count").asInt();
                classInfoList = JsonUtil.node2pojoList(result.get("data"), ClassInfo.class);
                break;
            case CodeConst.EXPAND_QUQXI_GET_XQ:
                //获取上课学期
                skxq = result.get("data").get("skxq").asText();
                break;

            case CodeConst.EXPAND_SIGNINFO:
                //获取签到课堂的信息
                if (status.equals("0")) {
                    classInfo.setKcmc(result.get("data").get("kcmc").asText());
                    classInfo.setLsmc(result.get("data").get("lsmc").asText());
                    classInfo.setSksj(result.get("data").get("sksj").asText());
                    classInfo.setKtxc(result.get("data").get("ktxc").asText());
                    student.setXsxm(result.get("data").get("xsxm").asText());
                    student.setXh(result.get("data").get("xh").asText());
                    student.setBj(result.get("data").get("bj").asText());
                }
                break;

            case CodeConst.EXPAND_BUQIAN:
                //补签成功，设为缺席成功
                if (status.equals("0")) {
                    //获取补签时间
                    signTime = result.get("data").get("qdsj").asText();
                    qdfs = result.get("data").get("qdfs").asText();
                }
                break;
        }
    }

    /**
     * 创建课堂
     *
     * @param kcmc        课堂名称
     * @param ktzt        课堂主题
     * @param skdd        上课地点
     * @param pzqd        拍照签到  （0为不，1为是）
     * @param requestCode
     */
    public void createClass(String kcmc, String ktzt, String skdd, String pzqd, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "createClass", true);
        Log.d("创建课堂:url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcmc", kcmc);
        params.add("ktzt", ktzt);
        params.add("skdd", skdd);
        params.add("pzqd", pzqd + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 创建课堂时获取课堂信息
     *
     * @param requestCode 请求码
     */
    public void getClass(int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "getClass", true);
        Log.d("获取课堂信息：url->" + url);
        RequestParams params = new RequestParams();
        postRequest(url, params, requestCode);
    }

    /**
     * 获取今日点名的列表
     *
     * @param requestCode 请求码
     */
    public void getTodayClassList(int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "todayClass", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("uid", Arad.preferences.getString(LocalMark.USER_UID.getName()));
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 控制签到按钮开关
     *
     * @param ktid        课程名称
     * @param open        签到开关（0关，1开）
     * @param requestCode 请求码
     */
    public void controlSign(String ktid, String open, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "controlSign", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ktid", ktid);
        params.add("open", open);
        postRequest(url, params, requestCode);
    }

    /**
     * 查看课堂详情
     *
     * @param ktid        课堂id
     * @param requestCode 请求码
     */
    public void LookClassInfo(String ktid, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "todayClassSign", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ktid", ktid);
//        params.add("pageNo", pageNo + "");
//        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 对个别学生进行补签或者设为未到
     *
     * @param kcid        课程ID
     * @param key         补签或者设为未到，补签为1，设为未到为0.
     * @param xh          学生的学号
     * @param ktid        课堂ID
     * @param requestCode 请求码
     */
    public void buQian(String ktid, String kcid, String xh, String key, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "nextSign", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("key", key);
        params.add("xh", xh);
        params.add("ktid", ktid);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取统计列表
     *
     * @param requestCode 请求码
     */
    public void getTongjiList(int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "courseList", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 获取历史课堂
     *
     * @param pageNo      当前页
     * @param pageSize    每页显示的条数
     * @param search      搜索关键字   课程名称
     * @param requestCode 请求码
     */
    public void getHistoryClass(int pageNo, int pageSize, String search, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "historyClass", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        params.add("search", search);
        postRequest(url, params, requestCode);
    }


    /**
     * 获取课程详情
     *
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void getCourseInfo(String kcid, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "historyDetail", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 添加学生
     *
     * @param kcid        课程id
     * @param name        学生姓名
     * @param xh          学生学号
     * @param bj          学生班级
     * @param requestCode 请求码
     */
    public void addStudent(String kcid, String name, String xh, String bj, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "addStudent", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("xm", name);
        params.add("xh", xh);
        params.add("bj", bj);
        postRequest(url, params, requestCode);
    }

    /**
     * 学生名单中删除学生
     *
     * @param kcid        课程id
     * @param xh          学生学号
     * @param requestCode 请求码
     */
    public void deleteStudent(String kcid, String xh, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "delStudent", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("xh", xh);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取学生列表（模糊查找）
     *
     * @param key         搜索关键字
     * @param requestCode 请求码
     */
    public void getStudentList(String kcid, int pageNo, int pageSize, String key, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "studentList", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        if (key != null)
            params.add("key", key);
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        Log.d("kcid=" + kcid);
        Log.d("key=" + key);
        Log.d("pageNo=" + pageNo);
        Log.d("pageSize=" + pageSize);
        postRequest(url, params, requestCode);
    }

    /**
     * 获取课程内学生列表
     *
     * @param ktid        课堂名称
     * @param key         搜索关键字
     * @param requestCode 请求码
     */
    public void getSignStudentList(String ktid, int pageNo, int pageSize, String key, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "studentList", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ktid", ktid);
        if (key != null)
            params.add("key", key);
//        params.add("pageNo", pageNo + "");
//        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 创建课程
     *
     * @param kcmc        课程名称
     * @param requestCode 请求码
     */
    public void createCourse(String kcmc, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "createCourse", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcmc", kcmc);
        postRequest(url, params, requestCode);
    }

    /**
     * 编辑课程
     *
     * @param kcid        课程id
     * @param kcmc        课程名称
     * @param requestCode 请求码
     */
    public void modifyCourse(String kcid, String kcmc, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "updateCourse", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("kcmc", kcmc);
        postRequest(url, params, requestCode);
    }

    /**
     * 删除课程
     *
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void delCourse(String kcid, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "delCourse", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        postRequest(url, params, requestCode);
    }

    /**
     * 缺席统计——某节课的缺席学生名单
     *
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void getquexiStudent(String kcid, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "notInclass", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("kcid", kcid);
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 缺席统计——缺席课堂，查询学生缺席的课堂
     *
     * @param xh          学生学号
     * @param kcid        课程id
     * @param requestCode 请求码
     */
    public void getquexiClass(String xh, String kcid, int pageNo, int pageSize, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "notComingclass", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("xh", xh);
        params.add("kcid", kcid);
        params.add("pageNo", pageNo + "");
        params.add("pageSize", pageSize + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 缺席统计——获取上课学期
     *
     * @param requestCode 请求码
     */
    public void getNowXueqi(int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "getNowXueqi", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        postRequest(url, params, requestCode);
    }

    /**
     * 获取签到列表
     *
     * @param history     1为历史签到，0为今日签到，2为教室查询。
     * @param search      搜索关键字，可为空。（history为0时关键字包括教室号/老师名称，为1时关键字为课程名称）
     * @param page        页码，从1开始。如果页码数不对，（比如一共13条数据要求每页10个，这边要求第3页），没有更多了，则返回值中data为空数组
     * @param num         每页返回数组个数。
     * @param requestCode 请求码
     */
    public void getMySign(int history, String search, int page, int num, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "mySign", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("page", page + "");
        params.add("history", history + "");
        params.add("num", num + "");
        if (!StringUtils.isNull(search)) {
            params.add("search", search);
        }
        postRequest(url, params, requestCode);
    }

    /**
     * 签到详情
     *
     * @param ktid        课堂id
     * @param requestCode 请求码
     */
    public void getSignInfo(String ktid, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "detail", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ktid", ktid);
        postRequest(url, params, requestCode);
    }

    /**
     * 签到动作
     *
     * @param ktid  课堂id
     * @param xsxm  学生姓名
     * @param xsxh  学生学号
     * @param xsbj  学生班级
     * @param qdfs  1扫码 0输入课堂号 2老师补签
     * @param sbbsm 设备标识码
     */
    public void goSign(String ktid, String xsxm, String xsxh, String xsbj, String qdfs, String sbbsm, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "sign", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ktid", ktid);
        params.add("xsxm", xsxm);
        params.add("xsxh", xsxh);
        params.add("xsbj", xsbj);
        params.add("qdfs", qdfs);
        params.add("sbbsm", sbbsm);
        Log.d("签到参数：" + params.toString());
        postRequest(url, params, requestCode);
    }

    /**
     * 签到动作
     *
     * @param ktid  课堂id
     * @param xsxm  学生姓名
     * @param xsxh  学生学号
     * @param xsbj  学生班级
     * @param qdfs  1扫码 0输入课堂号 2老师补签
     * @param sbbsm 设备标识码
     * @param file  课堂现场（照片）可有可无
     */
    public void goSignWithPhoto(String ktid, String xsxm, String xsxh, String xsbj, String qdfs, String sbbsm, File file, int requestCode) {
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Sign.toString(), "sign", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ktid", ktid);
        params.add("xsxm", xsxm);
        params.add("xsxh", xsxh);
        params.add("xsbj", xsbj);
        params.add("qdfs", qdfs);
        params.add("sbbsm", sbbsm);
        try {
            params.put("userpic", file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Log.d("签到参数：" + params.toString());
        postRequest(url, params, requestCode);
    }

    public ClassInfo getClassInfo() {
        return classInfo;
    }

    public List<ClassInfo> getClassInfoList() {
        return classInfoList;
    }

    public String getStatus() {
        return status;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getMdrs() {
        return mdrs;
    }

    public int getCount() {
        return count;
    }

    public String getStudentCount() {
        return studentCount;
    }

    public List<ClassInfo> getMyclasses() {
        return myclasses;
    }

    public String getSkxq() {
        return skxq;
    }

    public int getCountCallname() {
        return countCallname;
    }

    public Student getStudent() {
        return student;
    }

    public String getSignTime() {
        return signTime;
    }

    public String getQdfs() {
        return qdfs;
    }
}
